package com.mobi.Survey.utils;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.view.Display;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * Device.java This class provides device related information
 * 
 * 
 */
public class Device {
	private float height;
	private float width;

	private String deviceId;
	private static Device device;

	/**
	 * Calculate Device width and height in pixels
	 * 
	 * @param ctx
	 */
	private final void calculateDeviceDim(Context ctx) {
		Display dispaly = ((Activity) ctx).getWindowManager()
				.getDefaultDisplay();
		height = dispaly.getHeight();
		width = dispaly.getWidth();
	}

	public static final Device getInstance() {

		return (device == null) ? device = new Device() : device;

	}

	/**
	 * 
	 * @return Available disk space in MBs
	 */
	public static float getAvailableSDSpace() {
		StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
				.getPath());
		long bytesAvailable = (long) stat.getBlockSize()
				* (long) stat.getAvailableBlocks();
		return bytesAvailable / (1024.f * 1024.f);
	}

	/**
	 * 
	 * @return true if sd card is there
	 */
	public static boolean isSdPresentForReadWrite() {

		boolean mExternalStorageAvailable = false;
		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
			// We can read and write the media
			mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			// We can only read the media
			mExternalStorageAvailable = true;
			mExternalStorageWriteable = false;
		} else {
			// Something else is wrong. It may be one of many other states, but
			// all we need
			// to know is we can neither read nor write
			mExternalStorageAvailable = mExternalStorageWriteable = false;
		}

		if (mExternalStorageAvailable == mExternalStorageWriteable == true)
			return true;
		else
			return false;

	}

	/**
	 * Call this method once
	 * 
	 * @param ctx
	 */
	public void init(Context ctx) {

		calculateDeviceDim(ctx);
		findDeviceId(ctx);

	}

	/**
	 * 
	 * @param ctx
	 * @return true if device is connected to a network. This does't guarantee
	 *         that internet is also available
	 */
	public static boolean isNetworkAvailable(Context ctx) {
		NetworkInfo activeNetworkInfo = null;
		try {
			ConnectivityManager connectivityManager = (ConnectivityManager) ctx
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

			if (activeNetworkInfo == null) {
				return false;
			} else if (!activeNetworkInfo.isConnected()) {
				return false;
			} else if (!activeNetworkInfo.isAvailable()) {
				return false;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return true;
	}

	/**
	 * 
	 * @param ctx
	 */
	private final void findDeviceId(Context ctx)

	{
		TelephonyManager telephonyManager = (TelephonyManager) ctx
				.getSystemService(Context.TELEPHONY_SERVICE);
		device.deviceId = telephonyManager.getDeviceId();

	}

	/**
	 * 
	 * @param percentHeight
	 * @return height in pixels of given percent
	 */
	public final int getPercentHeight(float percentHeight) {

		return (int) (height * percentHeight);

	}

	/**
	 * 
	 * @param percentWidth
	 * @return width in pixels of given percent
	 */
	public int getPercentWidth(float percentWidth) {

		return (int) (width * percentWidth);

	}

	/**
	 * Before calling this method you should call {@link #findDeviceId(Context)}
	 * 
	 * @return device id
	 */
	public static final String getDeviceId() {
		return device.deviceId;
	}

}
